Function Inference

Sin Function


In [3]:
from keras.models import Sequential
from keras.layers import LSTM, Dense

import numpy as np
import math
%matplotlib inline
import matplotlib.pyplot as plt

model = Sequential()
model.add(Dense(100, input_dim=1, init='uniform',activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='tanh'))
model.compile(loss='mean_squared_error',optimizer='rmsprop',metrics=['accuracy'])

x_train = 2*math.pi*np.random.random((5000,1))
y_train = np.sin(x_train)

x_val = 2*math.pi*np.random.random((100,1))
y_val = np.sin(x_val)

model.fit(x_train,y_train, nb_epoch=100, batch_size=20,verbose=0,shuffle=True, validation_split=0.2)


Out[3]:
<keras.callbacks.History at 0x71bbaef0>

Prediction


In [4]:
out = model.predict(x_val)

Comparsion


In [5]:
plt.scatter(x_val, y_val,c='r',label='Accurate')
plt.scatter(x_val,out,label='Predicted')
plt.legend(loc=1)
plt.show()


Extension


In [6]:
x_ext = 6*math.pi*np.random.random((100,1))
out = model.predict(x_ext)
plt.scatter(x_ext,out,label='Predicted')
plt.legend(loc=1)
plt.show()


Linear Function


In [9]:
from keras.models import Sequential
from keras.layers import LSTM, Dense

import numpy as np
import math
%matplotlib inline
import matplotlib.pyplot as plt

model = Sequential()
model.add(Dense(100, input_dim=1, init='uniform',activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error',optimizer='rmsprop',metrics=['accuracy'])

x_train = 100*np.random.random((5000,1))
y_train = 50 + 0.2*x_train

x_val = 100*np.random.random((100,1))
y_val = 50 + 0.2*x_val

hist = model.fit(x_train,y_train, nb_epoch=100, batch_size=20,verbose=0,shuffle=True, validation_split=0.2)

out = model.predict(x_val)

plt.scatter(x_val, y_val,c='r',label='Accurate')
plt.scatter(x_val,out,label='Predicted')
plt.legend(loc=1)
plt.show()


Nonlinear Function


In [16]:
from keras.models import Sequential
from keras.layers import LSTM, Dense

import numpy as np
import math
%matplotlib inline
import matplotlib.pyplot as plt

model = Sequential()
model.add(Dense(100, input_dim=1, init='uniform',activation='linear'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='linear'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error',optimizer='rmsprop',metrics=['accuracy'])

x_train = 2*math.pi*np.random.random((5000,1))
y_train = 50*np.exp(np.sin(x_train))

x_val = 2*math.pi*np.random.random((100,1))
y_val = 50*np.exp(np.sin(x_val))

hist = model.fit(x_train,y_train, nb_epoch=200, batch_size=40,verbose=0,shuffle=True, validation_split=0.1)

out = model.predict(x_val)

plt.scatter(x_val, y_val,c='r',label='Accurate')
plt.scatter(x_val,out,label='Predicted')
plt.legend(loc=1)
plt.show()


Adjustable parameters

  • Type of Neural Network
  • Node number, Initilization, Activation Form, Dropout of each layer
  • Loss function, Optimizer type, Metrics function to compile network
  • Epoch, BatchSize, Shuffle, Validation Setting to fit data

In [ ]: